<template>
  <div>
    <el-form-item prop="customerId" style="margin-left: 70px" label="客户简称">
      <el-select v-model="customerId" :disabled="flag" style="width:77%;margin-left: 20px" filterable clearable remote :remote-method="remoteCustomer">
        <el-option
          v-for="item in customerIdOptions"
          :key="parseInt(item.id)"
          :label="item.text"
          :value="parseInt(item.id)"
        />
      </el-select>
    </el-form-item>
  </div>
</template>
<script>
import {ajax} from "@/store/yg/common/utils";
import * as constant from "@/store/yg/common/constant";

export default {
  data() {
    return {
      customerId:"",
      customerMap:"",
      flag:false,
      customerIdOptions: [],
    };
  },
  mounted() {
    this.customerMap = this.$store.state.edit.entry.sources[
      "customerSource"
      ];
    if (this.$route.query.pid!=null) {
      this.flag=true;
    }
    if (!this.$store.state.edit.row.id) {
      if (this.$route.query.pid!=null) {
        this.editData();
      }
    } else {
      this.customerId = this.$store.state.edit.row.customerId;
      this.customerIdOptions.push(
        {
          id:this.customerId,
          text: this.customerMap[this.customerId]
        }
      );
    }
  },
  watch: {
    'customerId': {
      handler(nVal) {
        this.$store.state.edit.row.customerId = nVal;
        this.customerIdOptions.push(
          {
            id:this.customerId,
            text: this.customerMap[this.customerId]
          }
        );
      }
    }
  },
  methods:{
    async editData(){
        const attrs = {
          class: "com.tofba.app.client.customer.CustomerInfoClient"
        };
        let res = await ajax("queryById", {
          id: this.$route.query.pid
        },attrs);
        if (res !== null) {
          this.customerId=res.id;
        }
    },
    init() {
      this.$store.state.edit.row.visitPlace = this.provincesModel;
    },
    async remoteCustomer(query){
      if (query !== '') {
        const attrs = {
          class: "com.tofba.app.client.statistics.StatementWaybillFileTypeClient"
        };
        this.customerIdOptions = await ajax("searchCustomerSelect", {
          name: query
        },attrs);
      }else {
        this.customerIdOptions = {};
      }
    },
    sleep(numberMillis) {
     var now = new Date();
     var exitTime = now.getTime() + numberMillis;
    while (true) {
       now = new Date();
       if (now.getTime() > exitTime)
      return;
      }
    }
  }

}
</script>

<style scoped>

</style>
